package com.ruoyi.inspection.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.inspection.constant.InspectionConstant;
import com.ruoyi.inspection.domain.GovernCheckTaskPlanItem;
import com.ruoyi.inspection.domain.vo.GovernCheckTaskPlanItemVo;
import com.ruoyi.inspection.service.GovernCheckTaskPlanItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专项任务计划检查项表(GovernCheckTaskPlanItem)表控制层
 *
 * @author makejava
 * @since 2024-06-19 16:16:21
 */
@Slf4j
@RestController
@Api("任务计划子项接口")
@RequestMapping("/check/governCheckTaskPlanItem")
public class GovernCheckTaskPlanItemController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private GovernCheckTaskPlanItemService governCheckTaskPlanItemService;
    @Autowired
    private TokenService tokenService;

    /**
     * 分页查询
     *
     * @param governCheckTaskPlanItem 筛选条件
     * @return 查询结果
     */
    @ApiOperation("查询任务计划子项列表")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:list')")
    @GetMapping("/list")
    public TableDataInfo queryByPage(GovernCheckTaskPlanItem governCheckTaskPlanItem) {
        startPage();
        List<GovernCheckTaskPlanItem> governCheckTaskPlanItems = this.governCheckTaskPlanItemService.queryByPage(governCheckTaskPlanItem);
        return getDataTable(governCheckTaskPlanItems);
    }

    /**
     * 分页查询
     *
     * @param governCheckTaskPlanItem 筛选条件
     * @return 查询结果
     */
    @ApiOperation("查询个人相关的子项")
//    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:list')")
    @GetMapping("/userSelf/list")
    public TableDataInfo queryByCheckUser(HttpServletRequest request, GovernCheckTaskPlanItem governCheckTaskPlanItem) {
//        governCheckTaskPlan.setCheckUserId(SecurityUtils.getUserId());
//        LoginUser user = tokenService.getLoginUser(request);
//        governCheckTaskPlan.setCheckUserId(user.getUserId());
        // 专项检查（市局）
//        governCheckTaskPlanItem.setIsGovernCheck("1");
        boolean zxgly = SecurityUtils.getLoginUser().getUser().getRoles().stream().map(SysRole::getRoleId).distinct().collect(Collectors.toList()).contains(InspectionConstant.ROLE_ZXGL_ID);
        if (getUserId()!=1 && !zxgly) {
            governCheckTaskPlanItem.setGovernCheckUserId(getUserId() + "");
        }

        startPage();
        List<GovernCheckTaskPlanItem> governCheckTaskPlanItems = this.governCheckTaskPlanItemService.queryByPage(governCheckTaskPlanItem);
        return getDataTable(governCheckTaskPlanItems);
    }

//    /**
//     * 分页查询
//     *
//     * @param governCheckTaskPlanItem 筛选条件
//     * @return 查询结果
//     */
//    @ApiOperation("查询排查任务")
//    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:list')")
//    @GetMapping("/pcrw/list")
//    public TableDataInfo queryTasks(HttpServletRequest request, GovernCheckTaskPlanItem governCheckTaskPlanItem) {
//        // 企业自主检查
//        governCheckTaskPlanItem.setIsGovernCheck("0");
//        boolean zxgly = SecurityUtils.getLoginUser().getUser().getRoles().stream().map(SysRole::getRoleId).distinct().collect(Collectors.toList()).contains(InspectionConstant.ROLE_ZXGL_ID);
//        if (getUserId()!=1 && !zxgly) {
//            governCheckTaskPlanItem.setGovernCheckUserId(getUserId() + "");
//        }
//
//        startPage();
//        List<GovernCheckTaskPlanItem> governCheckTaskPlanItems = this.governCheckTaskPlanItemService.queryTaskList(governCheckTaskPlanItem);
//        return getDataTable(governCheckTaskPlanItems);
//    }

    /**
     * 编辑数据
     *
     * @param governTaskPlanId
     * @return 编辑结果
     */
    @ApiOperation("批量重置子任务项")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:query')")
    @PostMapping("/resetCheckUser/{governTaskPlanId}")
    public AjaxResult resetCheckUser(@PathVariable Long governTaskPlanId) {
        return AjaxResult.success(this.governCheckTaskPlanItemService.resetCheckUser(governTaskPlanId));
    }

    /**
     * 编辑数据
     *
     * @param governCheckTaskPlanItem 实体
     * @return 编辑结果
     */
    @ApiOperation("修改子任务项")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody GovernCheckTaskPlanItem governCheckTaskPlanItem) {
        governCheckTaskPlanItem.setGovernUpdateBy(getUsername());
        return AjaxResult.success(this.governCheckTaskPlanItemService.update(governCheckTaskPlanItem));
    }

    /**
     * 删除数据
     *
     * @param governTaskPlanItemId 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除子任务项")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:remove')")
    @DeleteMapping("/{governTaskPlanItemId}")
    public AjaxResult deleteById(@PathVariable Long governTaskPlanItemId) {
        return AjaxResult.success(this.governCheckTaskPlanItemService.deleteById(governTaskPlanItemId));
    }

    /**
     * 批量修改
     *
     * @param governCheckTaskPlanItems
     * @return
     */
    @ApiOperation("批量分批子任务项")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:edit')")
    @PostMapping("/batchUpdate")
    public AjaxResult batchUpdate(@RequestBody List<GovernCheckTaskPlanItem> governCheckTaskPlanItems) {
        for (GovernCheckTaskPlanItem governCheckTaskPlanItem : governCheckTaskPlanItems) {
            governCheckTaskPlanItemService.update(governCheckTaskPlanItem);
        }
        return AjaxResult.success();
    }

    /**
     * 查询检查详情
     *
     * @param governTaskPlanItemId
     * @return 编辑结果
     */
    @ApiOperation("查询检查子任务详情")
    @GetMapping("/api/detail/{governTaskPlanItemId}")
    public AjaxResult detail( @PathVariable Long governTaskPlanItemId) {
        return AjaxResult.success(this.governCheckTaskPlanItemService.selectTaskPlanItemDetail(governTaskPlanItemId));
    }

    /**
     * 查询检查详情
     *
     * @param governTaskPlanItemId
     * @return 编辑结果
     */
    @ApiOperation("排查任务详情")
    @GetMapping("/api/taskDetail/{governTaskPlanItemId}")
    public AjaxResult taskDetail( @PathVariable Long governTaskPlanItemId) {
        return AjaxResult.success(this.governCheckTaskPlanItemService.getTaskDetail(governTaskPlanItemId));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:query')")
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        return AjaxResult.success(this.governCheckTaskPlanItemService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param governCheckTaskPlanItem 实体
     * @return 新增结果
     */
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:add')")
    @PostMapping
    public AjaxResult add(GovernCheckTaskPlanItem governCheckTaskPlanItem) {
        return AjaxResult.success(this.governCheckTaskPlanItemService.insert(governCheckTaskPlanItem));
    }

    /**
     * 导出任务检查项
     *
     * @param response
     * @param governTaskPlanId
     */
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:export')")
    @Log(title = "导出任务检查项", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{governTaskPlanId}")
    public void export(HttpServletResponse response, @PathVariable Long governTaskPlanId) {
        List<GovernCheckTaskPlanItemVo> list = governCheckTaskPlanItemService.queryByTaskPlanId(governTaskPlanId);
        ExcelUtil<GovernCheckTaskPlanItemVo> util = new ExcelUtil<>(GovernCheckTaskPlanItemVo.class);

        util.exportExcel(response, list, "任务检查项");
    }

    /**
     * 导入
     *
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "导入检查人员", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('survey:governCheckTaskPlanItem:export')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<GovernCheckTaskPlanItemVo> util = new ExcelUtil<>(GovernCheckTaskPlanItemVo.class);
        List<GovernCheckTaskPlanItemVo> list = util.importExcel(file.getInputStream());


        String message = governCheckTaskPlanItemService.allocateCheckUser(list, getUsername());
        return AjaxResult.success(message);
    }

    @Log(title = "导入检查人员", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('survey:governCheckTaskPlanItem:export')")
    @PostMapping("/importData2")
    public AjaxResult importData2(MultipartFile file) throws Exception {
        ExcelUtil<GovernCheckTaskPlanItemVo> util = new ExcelUtil<>(GovernCheckTaskPlanItemVo.class);
        List<GovernCheckTaskPlanItemVo> list = util.importExcel(file.getInputStream());


        String message = governCheckTaskPlanItemService.allocateCheckUser2(list, getUsername());
        return AjaxResult.success(message);
    }


    /**
     * 新增计划检查项
     *
     * @param governCheckTaskPlanItem
     * @return 新增结果
     */
    @ApiOperation("新增计划检查项")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlanItem:edit')")
    @PostMapping("/addPlanItems")
    public AjaxResult addPlanItems(@RequestBody GovernCheckTaskPlanItem governCheckTaskPlanItem) {
        return AjaxResult.success(this.governCheckTaskPlanItemService.addCheckTaskPlanItems(governCheckTaskPlanItem, getUsername()));
    }

//    /**
//     * 导入试题
//     *
//     * @param file
//     * @param surveyId
//     * @return
//     * @throws Exception
//     */
//    @PreAuthorize("@ss.hasPermi('survey:question:import')")
//    @Log(title = "导入问卷试题", businessType = BusinessType.IMPORT)
//    @PostMapping("/importData")
//    public AjaxResult importData(MultipartFile file, Long surveyId) throws Exception {
//        ExcelUtil<SurveyQuestionVdm> util = new ExcelUtil<>(SurveyQuestionVdm.class);
//        List<SurveyQuestionVdm> questionList = util.importExcel(file.getInputStream());
//
//        String message = questionService.importQuestions(questionList, getUsername(), surveyId);
//        return AjaxResult.success(message);
//    }
//
//    /**
//     * 导出试题
//     *
//     * @param response
//     * @param surveyId
//     */
//    @PreAuthorize("@ss.hasPermi('survey:question:export')")
//    @Log(title = "导出问卷试题", businessType = BusinessType.EXPORT)
//    @GetMapping("/export/{surveyId}")
//    public void export(HttpServletResponse response, @PathVariable Long surveyId) {
//        List<SurveyQuestionVdm> list = questionService.selectSurveyQuestionExportData(surveyId);
//        ExcelUtil<SurveyQuestionVdm> util = new ExcelUtil<>(SurveyQuestionVdm.class);
//
//        util.exportExcel(response, list, "问卷题目数据");
//    }
}

